package com.vivien.example.springbootdemo.service;

import com.vivien.example.springbootdemo.dao.OrderDao;
import com.vivien.example.springbootdemo.dao.PersonDao;
import com.vivien.example.springbootdemo.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private PersonDao personDao;

    @Transactional(propagation = Propagation.REQUIRED)
    public OrderResponse getOrderList(OrderRequest orderRequest){
        List<Order> orders = orderDao.getOrderList(orderRequest);
        Integer count = orderDao.getOrderCount(orderRequest);
        // 添加映射人名
        List<String> identifyNoList = new LinkedList<>();
        for(Order o:orders){
            identifyNoList.add(o.getPolicyholderId());
            identifyNoList.add(o.getInsuredPersonId());
            identifyNoList.add(o.getInsuerId());
            identifyNoList.add(o.getBeneficiaryId());
        }

        // 映射人名，再次执行sql
        List<Person> persons = personDao.getPersonList(identifyNoList);
        Map<String,Person> personMapping = new HashMap<>();
        for(Person p:persons){
            personMapping.put(p.getIdentifyNo(),p);
        }
//        System.out.println("---------------------");
//        for(String key:personMapping.keySet()){
//            System.out.println(key+":"+personMapping.get(key));
//        }
//        System.out.println("---------------------");

        for(Order o:orders){
            o.setPolicyholderName(personMapping.get(o.getPolicyholderId()).getName());
            o.setInsuredPersonName(personMapping.get(o.getInsuredPersonId()).getName());
            o.setInsuerName(personMapping.get(o.getInsuerId()).getName());
            o.setBeneficiaryName(personMapping.get(o.getBeneficiaryId()).getName());
        }
        System.out.println("+++++++++++++++++");
        for(Order o:orders){
            System.out.println(o);
        }
        System.out.println("+++++++++++++++++");

        return new OrderResponse(orders,count);
    }

    public OrderDetail getOrderById(String orderId){
        OrderDetail orderDetail =  orderDao.getOrderById(orderId);
        List<String> identifyNoList = new LinkedList<>();

        identifyNoList.add(orderDetail.getPolicyholderId());
        identifyNoList.add(orderDetail.getInsuredPersonId());
        identifyNoList.add(orderDetail.getInsuerId());
        identifyNoList.add(orderDetail.getBeneficiaryId());
        identifyNoList.add(orderDetail.getAgentId());

        // 映射人名，再次执行sql
        List<Person> persons = personDao.getPersonList(identifyNoList);
        Map<String,Person> personMapping = new HashMap<>();
        for(Person p:persons){
            personMapping.put(p.getIdentifyNo(),p);
        }

        orderDetail.setPolicyholderName(personMapping.get(orderDetail.getPolicyholderId()).getName());
        orderDetail.setInsuerName(personMapping.get(orderDetail.getInsuerId()).getName());
        orderDetail.setInsuredPersonName(personMapping.get(orderDetail.getInsuredPersonId()).getName());
        orderDetail.setBeneficiaryName(personMapping.get(orderDetail.getBeneficiaryId()).getName());
        orderDetail.setAgentName(personMapping.get(orderDetail.getAgentId()).getName());

        // 映射任命，再次执行sql
        return orderDetail;
    }
}
